Imports Microsoft.VisualBasic

Public Class Field
    Public Updated As Boolean
    Public IsNull As Boolean
    Public Loaded As Boolean
    Public Entity As Entity

    Protected Sub SetUpdated()
        Me.Updated = True
    End Sub

    Protected Sub Load()
        Entity.Load()
    End Sub

    Public Class Text
        Inherits Field

        Private _value As String

        Public Property value() As String
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
                SetUpdated()
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function

        Public Shared Widening Operator CType(ByVal str As String) As Field.Text
            Dim fi As New Field.Text
            fi.Updated = True
            fi.value = str
            Return fi
        End Operator
    End Class

    Public Class Binary
        Inherits Field

        Private _value As Byte()

        Public Property value() As Byte()
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Byte())
                _value = value
                SetUpdated()
            End Set
        End Property
    End Class

    Public Class Bit
        Inherits Field

        Private _value As Boolean

        Public Property value() As Boolean
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Boolean)
                _value = value
                SetUpdated()
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function

        Public Shared Widening Operator CType(ByVal i As Boolean) As Field.Bit
            Dim fi As New Field.Bit
            fi.value = i
            fi.Updated = True
            Return fi
        End Operator
    End Class

    Public Class Int
        Inherits Field

        Private _value As Integer

        Public Property value() As Integer
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
                SetUpdated()
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function

        Public Shared Widening Operator CType(ByVal i As Integer) As Field.Int
            Dim fi As New Field.Int
            fi.value = i
            fi.Updated = True
            Return fi
        End Operator
    End Class

    Public Class TinyInt
        Inherits Field

        Private _value As Integer

        Public Property value() As Integer
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
                SetUpdated()
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class

    Public Class Float
        Inherits Field

        Private _value As Double

        Public Property value() As Double
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Double)
                _value = value
                SetUpdated()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class

    Public Class DateTime
        Inherits Field

        Private _value As System.DateTime

        Public Property value() As System.DateTime
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As System.DateTime)
                _value = value
                SetUpdated()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class

    Public Class SmallDateTime
        Inherits Field

        Private _value As Date

        Public Property value() As Date
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Date)
                _value = value
                SetUpdated()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class

    Public Class SmallInt
        Inherits Field

        Private _value As Integer

        Public Property value() As Integer
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
                SetUpdated()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class

    Public Class [Decimal]
        Inherits Field

        Private _value As Decimal

        Public Property value() As Decimal
            Get
                If Not Loaded Then
                    Load()
                End If
                Return _value
            End Get
            Set(ByVal value As Decimal)
                _value = value
                SetUpdated()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Me.value.ToString
        End Function
    End Class
End Class


<AttributeUsage(AttributeTargets.All, AllowMultiple:=False, Inherited:=False)> _
Public Class FieldAttribute
    Inherits Attribute

    Public Size As Integer
    Public ColumnType As Data.SqlDbType
    Public Unicode As Boolean
    Public Variable As Boolean
    Public ColumnName As String

End Class

